<template>
  <div class="problemAdd">
    <a-form :form="form" @submit="problemSubmit" autoComplete="off">
      <!-- <a-row :gutter="24">
        <a-col :span="10"> -->
      <a-form-item label="标题">
        <a-textarea
          allow-clear
          placeholder="请输入标题！"
          :auto-size="{ minRows: 1, maxRows: 3 }"
          v-decorator="[
            'title',
            {
              rules: [{ required: true, message: '请输入标题！' }],
            },
          ]"
        />
      </a-form-item>
      <!-- </a-col>
      </a-row> -->
      <a-form-item label="内容">
        <Editorbar
          ref="editor"
          :disabled="true"
          v-model="content"
          placeholder="请输入内容"
          style="width: 100%"
        ></Editorbar>
      </a-form-item>
    </a-form>
    <div class="footBtn" style="margin-bottom: 20px">
      <a-button type="primary" @click="jump" style="margin-right: 24px">
        <a-icon type="rollback" />返回</a-button
      >
      <a-button type="primary" @click="problemSubmit" :disabled="false">
        <a-icon type="plus" />提交
      </a-button>
    </div>
  </div>
</template>
<script>
import Editorbar from "../../components/WangEditor";
import { getAction, putAction, postAction } from "../../http/manage";
export default {
  components: {
    Editorbar,
  },
  data() {
    return {
      content: "",
      question: "",
      form: this.$form.createForm(this),
      url: {
        addList: "/entrepreneurship/entrepreneurshipAdd",
        GetById: "/entrepreneurship/entrepreneurshipGetById/",
        Update: "/entrepreneurship/entrepreneurshipUpdate",
      },
    };
  },
  mounted() {
    if (this.$route.query.isAdd === "修改") {
      this.GetById();
    }
  },
  methods: {
    problemSubmit() {
      this.form.validateFields((err, values) => {
        if (!err) {
          var data = {
            content: this.content,
            title: values.title,
            category: this.$route.query.category,
          };
          console.log(values);
          if (this.$route.query.isAdd === "新增") {
            if (this.content.length > 0) {
              postAction(this.url.addList, data).then((res) => {
                if (res.data.code == 0) {
                  this.$message.success("提交成功");
                  this.$router.push({
                    path: "/VentureFinancing",
                    query: { key: this.$route.query.category },
                  });
                }
              });
            } else {
              this.$message.warning("请输入完整的内容");
            }
          } else {
            if (this.content.length > 0) {
              data['id']=this.$route.query.id
              putAction(this.url.Update, data).then((res) => {
                if (res.data.code == 0) {
                  this.$message.success("修改成功");
                  this.$router.push({
                    path: "/VentureFinancing",
                    query: { key: this.$route.query.category },
                  });
                }
              });
            } else {
              this.$message.warning("请输入完整的内容");
            }
          }
        }
      });
    },
    jump() {
      this.$router.push({
        path: "/VentureFinancing",
        query: { key: this.$route.query.category },
      });
    },
    GetById() {
      getAction(this.url.GetById + this.$route.query.id).then((res) => {
        if (res.data.code == 0) {
          let data = res.data.data;
          this.$nextTick(() => {
            this.form.setFieldsValue(_.pick(data, ["title"]));
            this.content = data.content;
          });
        }
      });
    },
  },
};
</script>
<style scoped>
.problemAdd {
  background-color: #fff;
  padding: 24px 24px 8px;
  border-radius: 4px;
  box-shadow: 0px 2px 6px 0px rgba(0, 0, 0, 0.12);
}
</style>
